package messageAsynchrone;

import java.io.IOException;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicPublisher;
import javax.jms.TopicSession;
import javax.jms.TopicSubscriber;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;

//import org.apache.activemq.command.ActiveMQMessage;
public class TestTopicAsyn {

	

	public static void main(String[] args) throws JMSException, IOException,
			InterruptedException {
		// TODO Auto-generated method stub
		ApplicationContext appContext = new FileSystemXmlApplicationContext(
				"C:\\Users\\stage1\\Desktop\\activMQ\\spring-jms.xml");
		MessageListnr listnr = (MessageListnr) appContext.getBean("myListener");

		TopicConnectionFactory topicConnectionFactory = (TopicConnectionFactory) listnr
			.getMytemplate().getConnectionFactory();
		//TopicConnectionFactory topicConnectionFactory=(TopicConnectionFactory) appContext.getBean("amqConnectionFactory");
		
		TopicConnection tc = topicConnectionFactory.createTopicConnection();

		TestTopicAsyn obj = new TestTopicAsyn();

		//
		obj.publisher(tc, listnr.getMytopic(), "myMessage!!!!!!!+++++!!!!!!!");
		System.out.println("fin publishing!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
		//obj.subscribe(tc, listnr.getMytopic(), listnr);

		//

	}

	void subscribe(TopicConnection topicConnection, Topic topic,
			MessageListnr ml) throws JMSException {
		TopicSession tsession = topicConnection.createTopicSession(false,
				Session.AUTO_ACKNOWLEDGE);
		TopicSubscriber ts = tsession.createSubscriber(topic);

		ts.setMessageListener(ml);
		System.out.println("okk;;;;;0000000000;");

	}

	void publisher(TopicConnection topicConnection, Topic topic,
			String mymessage) throws JMSException, InterruptedException {
		TopicSession tsession = topicConnection.createTopicSession(false,
				Session.AUTO_ACKNOWLEDGE);
		TopicPublisher topicPublisher = tsession.createPublisher(topic);
		topicConnection.start();
		TextMessage msg = (TextMessage) tsession.createTextMessage();
		int i = 0;
		while (i < 20) {
			i++;
			msg.setText(mymessage+i);
			System.out.println("je publie;;;;");
			topicPublisher.publish(msg);
			// wait(10000);
			System.out.println("c fait!!!!!!!!!!!!!!!!!!!!!!");
		}
	}



	
	
	
}
